package com.jeehentor.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeehentor.common.exception.HentorException;
import com.jeehentor.dto.SaveRoleDTO;
import com.jeehentor.entity.SysMenu;
import com.jeehentor.entity.SysRole;
import com.jeehentor.entity.SysRoleMenu;
import com.jeehentor.system.mapper.SysRoleMapper;
import com.jeehentor.system.service.ISysMenuService;
import com.jeehentor.system.service.ISysRoleMenuService;
import com.jeehentor.system.service.ISysRoleService;
import com.jeehentor.utils.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author wzd
 * @since 2024-04-08
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private final ISysRoleMenuService sysRoleMenuService;

    private final ISysMenuService menuService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRole(SaveRoleDTO roleModel) {
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleLambdaQueryWrapper.eq(SysRole::getRoleCode,roleModel.getRoleCode());
        List<SysRole> list = list(sysRoleLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            throw new HentorException("角色编码已存在");
        }
        // 将sysRole保存到数据库
        roleModel.setCreateTime(LocalDateTime.now());
        roleModel.setCreateBy(SecurityUtil.getCurrentUser().getId());
        save(roleModel);
        // 如果roleModel的menu列表不为空，将sysRole和menu列表传入sysRoleMenuService的saveRoleMenu方法
        if(CollectionUtil.isNotEmpty(roleModel.getMenu())){
            sysRoleMenuService.saveRoleMenu(roleModel.getId(),roleModel.getMenu());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateRole(SaveRoleDTO roleModel) {
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleLambdaQueryWrapper.eq(SysRole::getRoleCode,roleModel.getRoleCode());
        List<SysRole> list = list(sysRoleLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            if(!list.get(0).getId().equals(roleModel.getId())){
                throw new HentorException("角色编码已存在");
            }
        }
        roleModel.setUpdateBy(SecurityUtil.getCurrentUser().getId());
        roleModel.setUpdateTime(LocalDateTime.now());
        // 更新sysRole
        updateById(roleModel);
        // 如果roleModel的menu列表不为空，将sysRole和menu列表传入sysRoleMenuService的saveRoleMenu方法
        if(CollectionUtil.isNotEmpty(roleModel.getMenu())){
            LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId,roleModel.getId());
            sysRoleMenuService.remove(sysRoleMenuLambdaQueryWrapper);
            // 保存新的角色菜单关联关系
            sysRoleMenuService.saveRoleMenu(roleModel.getId(),roleModel.getMenu());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRole(SaveRoleDTO roleModel) {
        removeById(roleModel.getId());
        LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId,roleModel.getId());
        sysRoleMenuService.remove(sysRoleMenuLambdaQueryWrapper);
    }

    @Override
    public SaveRoleDTO roleDetail(SaveRoleDTO roleModel) {
        // 创建SaveRoleModel对象
        SaveRoleDTO saveRoleDTO = new SaveRoleDTO();
        // 获取角色模型
        SysRole byId = getById(roleModel);
        // 将角色模型的属性复制到SaveRoleModel对象
        BeanUtils.copyProperties(byId, saveRoleDTO);
        // 查询sysRoleMenu
        LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询角色模型ID相同的sysRoleMenu
        sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId,roleModel.getId());
        List<SysRoleMenu> list = sysRoleMenuService.list(sysRoleMenuLambdaQueryWrapper);
        LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询角色模型ID相同且状态为1的sysMenu
        sysMenuLambdaQueryWrapper.in(SysMenu::getId,list.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList()));
        sysMenuLambdaQueryWrapper.eq(SysMenu::getStatus,'1');
        List<SysMenu> menus = menuService.list(sysMenuLambdaQueryWrapper);
        // 将查询到的sysMenu构建成树
        saveRoleDTO.setMenu(menuService.buildTree(menus));
        // 返回SaveRoleModel对象
        return saveRoleDTO;
    }
}
